Reference

public member function
<memory>

std::auto_ptr::release

X* release() throw();
Release pointer
Sets the auto_ptr internal pointer to null pointer (which indicates it points to no object) without destructing the object currently pointed by the auto_ptr.

To force a destruction of the object pointed, use member function reset() instead.

The function returns a pointer to the object it pointed before the call, which is no longer its responsibility to destruct.

Parameters

none

Return value

A pointer to the element pointed by the auto_ptr object before the call. After the call, the internal pointer's value is the null pointer (points to no object).
X is auto_ptr's template parameter (i.e., the type pointed).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// auto_ptr::release example
#include <iostream>
#include <memory>
int main () {
 std::auto_ptr<int> auto_pointer (new int);
 int * manual_pointer;
 *auto_pointer=10;
 manual_pointer = auto_pointer.release();
 std::cout << "manual_pointer points to " << *manual_pointer << '\n';
 // (auto_pointer is now null-pointer auto_ptr)
 delete manual_pointer; 
 return 0;
}

Output:

manual_pointer points to 10


See also

auto_ptr::reset
Deallocate object pointed and set new value (public member function)
auto_ptr::get
Get pointer (public member function)
auto_ptr::operator=
Release and copy auto_ptr (public member function)

AltStyle によって変換されたページ (->オリジナル) /